package com.ruoyi.mytest.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.mytest.domain.MAuthor;
import com.ruoyi.mytest.domain.MType;
import com.ruoyi.mytest.domain.MZuoqu;
import com.ruoyi.mytest.mapper.MAuthorMapper;
import com.ruoyi.mytest.mapper.MTypeMapper;
import com.ruoyi.mytest.mapper.MZuoquMapper;
import com.ruoyi.mytest.vo.MMusicVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mytest.mapper.MMusicMapper;
import com.ruoyi.mytest.domain.MMusic;
import com.ruoyi.mytest.service.IMMusicService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author li
 * @date 2023-03-07
 */
@Service
public class MMusicServiceImpl implements IMMusicService {
    @Autowired
    private MMusicMapper mMusicMapper;

    @Autowired
    private MAuthorMapper mAuthorMapper;

    @Autowired
    private MTypeMapper mTypeMapper;

    @Autowired
    private MZuoquMapper mZuoquMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param musicId 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public MMusic selectMMusicByMusicId(Long musicId) {
        return mMusicMapper.selectMMusicByMusicId(musicId);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param mMusicVO 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<MMusicVO> selectMMusicList(MMusicVO mMusicVO) {
        System.out.println("music------------"+mMusicVO.toString());
        MMusic mMusic = new MMusic();
        BeanUtils.copyProperties(mMusicVO,mMusic);
        List<MMusic> musicList = mMusicMapper.selectMMusicList(mMusic);
        List<MMusicVO> list = musicList.stream().map(item -> {
            MMusicVO musicVO = new MMusicVO();
            BeanUtils.copyProperties(item, musicVO);
            MAuthor mAuthor = mAuthorMapper.selectMAuthorByAId(item.getMusicAuthorId());
            musicVO.setAName(mAuthor.getaName());
            MType mType = mTypeMapper.selectMTypeByTId(item.getMusicTypeId());
            musicVO.setTName(mType.gettName());
            MZuoqu mZuoqu = mZuoquMapper.selectMZuoquByZId(item.getMusicZuoquId());
            musicVO.setZName(mZuoqu.getzName());
            return musicVO;
        }).collect(Collectors.toList());
        System.out.println(list.toString()+"------------musicVoList");
        return list;
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param mMusicVo 【请填写功能名称】
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMMusic(MMusicVO mMusicVo) {
        System.out.println(mMusicVo.toString());
        // 添加作者
        MAuthor mAuthor = new MAuthor();
        mAuthor.setaName(mMusicVo.getAName());
        int authorId = mAuthorMapper.insertMAuthor(mAuthor);
        Long aId = Long.valueOf(authorId+"");
        // 添加作曲
        MZuoqu mZuoqu = new MZuoqu();
        mZuoqu.setzName(mMusicVo.getZName());
        Long zuoQuId = Long.valueOf(mZuoquMapper.insertMZuoqu(mZuoqu));
        // 添加歌曲类型
        MType mType = new MType();
        mType.settName(mMusicVo.getTName());
        Long typeId = Long.valueOf(mTypeMapper.insertMType(mType));
        // 添加music类
        MMusic mMusic = new MMusic();
        BeanUtils.copyProperties(mMusicVo, mMusic);
        mMusic.setMusicAuthorId(aId);
        mMusic.setMusicZuoquId(zuoQuId);
        mMusic.setMusicTypeId(typeId);
        System.out.println(mMusic.toString());
        mMusicMapper.insertMMusic(mMusic);

        return 1;
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param mMusic 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateMMusic(MMusic mMusic) {
        return mMusicMapper.updateMMusic(mMusic);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param musicIds 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteMMusicByMusicIds(Long[] musicIds) {
        return mMusicMapper.deleteMMusicByMusicIds(musicIds);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param musicId 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteMMusicByMusicId(Long musicId) {
        return mMusicMapper.deleteMMusicByMusicId(musicId);
    }
}
